package ListNodePractice;

import java.util.List;

public class MyLinkedList {
    int size;
    ListNode head;

    public MyLinkedList() {
        this.size = 0;
        this.head = null;
    }

    @Override
    public String toString() {
        return "MyLinkedList{" +
                "size=" + size +
                ", head=" + head +
                '}';
    }

    public int get(int index) {
        if (index < 0 || index >= size || this.head == null) {
            return -1;
        }
        ListNode tempNode = this.head;
        for (int i = 0; i < index; i++) {
            tempNode = tempNode.next;
        }
        return tempNode.val;
    }

    public void addAtHead(int val) {
        ListNode node = new ListNode(val);
        node.next = this.head;
        //head往前移动
        this.head = node;
        size++;
    }

    public void addAtTail(int val) {
        if (this.size == 0) {
            this.head = new ListNode(val);
            this.head.next = null;
            size++;
        } else {
            ListNode tempNode = this.head;
            while (tempNode.next != null) {
                tempNode = tempNode.next;
            }
            ListNode tailNode = new ListNode(val);
            tailNode.next = null;
            tempNode.next = tailNode;
            size++;
        }
    }

    public void addAtIndex(int index, int val) {
        if (index <= 0) {
            //如果为空的话添加到头部
            addAtHead(val);
            return;
        }
        //如果已经满了，那么就直接return
        if (this.size < index) {
            return;
        }
        //如果是最后一个,那么就在最后面添加一个元素
        if (this.size == index) {
            addAtTail(val);
            return;
        }
        ListNode tempNode = this.head;
        for (int i = 0; i < index - 1; i++) {
            tempNode = tempNode.next;
        }
        ListNode listNode = new ListNode(val);
        listNode.next = tempNode.next;
        tempNode.next = listNode;
        size++;
    }

    public void deleteAtIndex(int index) {

        if (index < 0 || index >= this.size) {
            return;
        }
        //如果删除的是第一个节点
        if (index == 0) {
            if (size != 1) {
                this.head =this.head.next;
                size--;
                return;
            }else {
                this.head = null;
                size--;
                return;
            }
        }

        ListNode tempNode = this.head;
        for (int i = 0; i < index - 1; i++) {
            tempNode = tempNode.next;
        }
        tempNode.next = tempNode.next.next;
        size--;
    }

    public static void main(String[] args) {
        MyLinkedList linkedList = new MyLinkedList();
        linkedList.addAtHead(1);
        System.out.println(linkedList.toString());
        linkedList.addAtTail(3);
        linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
        linkedList.get(1);            //返回2
        linkedList.deleteAtIndex(1);  //现在链表是1-> 3
        linkedList.get(1);            //返回3

        System.out.println(linkedList.toString());
    }
}
